package Utils;

import java.io.*;

/**
 * @author tieyi  2019/12/21 23:14
 * @version 1.0.0
 * @Description: No Description
 * @package Utils
 * @file Flink_UDF_Demo
 */
public class SQLReader {
    public static String readSQLFileByBytes(String fileName) throws Exception {
        File file = new File(fileName);
        InputStream in = null;
        StringBuffer sb = new StringBuffer();
        if (file.isFile() && file.exists()) { //判断文件是否存在
            System.out.println("以字节为单位读取文件内容，一次读多个字节：");
            // 一次读多个字节
            byte[] tempbytes = new byte[1024];
            int byteread = 0;
            in = new FileInputStream(file);
            SQLReader.showAvailableBytes(in);
            // 读入多个字节到字节数组中，byteread为一次读入的字节数
            while ((byteread = in.read(tempbytes)) != -1) {
                //  System.out.write(tempbytes, 0, byteread);
                String str = new String(tempbytes, 0, byteread);
                sb.append(str);
            }
        } else {
            System.out.println("找不到指定的文件，请确认文件路径是否正确");
        }
        return sb.toString();
    }
    private static void showAvailableBytes(InputStream in) {
        try {
            System.out.println("当前字节输入流中的字节数为 : "+in.available());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
